<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            margin: 100px 0 20px 0;
            display: inline-block;
            width: 50px;
            height: 50px;
            background-color: black;
            color: aliceblue;
            text-align: center;
            line-height: 50px;
            border-radius: 10px;
            font-size: 20px;
        }
    </style>
</head>
<body>
    
   
    <span class="hour">1</span>
    <span class="minute">2</span>
    <span class="second">3</span> <br>
    <button class="begin">开始倒计时</button>
    <button class="stop">停止倒计时</button>
</body>
<script>
    var hour = document.querySelector('.hour')
    var minute = document.querySelector('.minute')
    var second = document.querySelector('.second')
    var inputTime = +new Date('2021-6-2 18:00:00') //返回的是用户输入的时间
    countdown()
    var countDown = null
    

    function countdown(){
        var nowTime = +new Date()   //返回当前时间总的毫秒数
        var times = (inputTime - nowTime) / 1000 //剩余时间的秒数
        var h = parseInt(times / 60 / 60 % 24)
        h = h < 10 ? '0' + h : h
        hour.innerHTML = h
        var m = parseInt(times / 60 % 60)
        m = m < 10 ? '0' + m : m
        minute.innerHTML = m
        var s = parseInt(times % 60)
        s = s < 10 ? '0' + s : s                                                           
        second.innerHTML = s   
    }
    var begin = document.querySelector('.begin')
    var stop = document.querySelector('.stop')
    begin.addEventListener('click',function(){
        countDown = setInterval(countdown,1000)
    })
    stop.addEventListener('click',function(){
        clearInterval(countDown)
    })
</script>
</html>